﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using COFE.Core;
using COFE.Core.Utils;
using COFE.Data.Profile;
using NUnit.Framework;
using COFE.Core.Lister;
using COFE.Core.Entry;

namespace COFE.Data
{
    public static class IDBEntryManagerExtension
    {

        public static bool Exists(this IDBEntryManager entryManager, IUserProfile profile, Guid entryId)
        {
            return COFEDBShared.DBOperations.ReadDatabase(
                (context) => entryManager.LookupEntry(context, profile, entryId) != null);            
        }

        public static bool Exists(this IDBEntryManager entryManager, IUserProfile profile, string fullParseName, out Guid entryId)
        {
            var entry = COFEDBShared.DBOperations.ReadDatabase(
                (context) =>
                    entryManager.LookupEntry(context, profile, fullParseName));
            entryId = entry == null ? Guid.Empty : entry.EntryId;
            return entry != null;
        }

        public static bool Exists(this IDBEntryManager entryManager, Guid entryId)
        {
            return entryManager.Exists(COFEShared.AdminUserProfile, entryId);
        }

        public static bool Exists(this IDBEntryManager entryManager,string fullParseName, out Guid entryId)
        {
            return entryManager.Exists(COFEShared.AdminUserProfile, fullParseName, out entryId);
        }

        public static bool Exists(this IDBEntryManager entryManager, string fullParseName)
        {
            Guid entryId;
            return entryManager.Exists(COFEShared.AdminUserProfile, fullParseName, out entryId);
        }

        #region LookupEntry with context with profile
        public static Entry LookupEntry(this IDBEntryManager entryManager, COFEDB2 context, IUserProfile profile, Guid entryId)
        {
            return entryManager.LookupEntry(context, profile, entryId);
        }

        public static Entry LookupEntry(this IDBEntryManager entryManager, COFEDB2 context, IUserProfile profile, string fullParseName)
        {            
            return entryManager.LookupEntry(context, profile, fullParseName);
        }
        #endregion

        #region LookupEntry without context with profile

        public static string Lookup(this IDBEntryManager entryManager, Guid entryId)
        {
            return COFEDBShared.DBOperations.ReadDatabase((context) =>
                {
                    var dbEntry = entryManager.LookupEntry(context, entryId);
                    if (dbEntry != null)
                        return COFEPath.FormatFullParseName(dbEntry.PrimaryVolumeId, dbEntry.ParsePath);
                    return null;
                });
        }

        public static Guid Lookup(this IDBEntryManager entryManager, string fullParseName)
        {
            return COFEDBShared.DBOperations.ReadDatabase((context) =>
                entryManager.LookupEntry(context, fullParseName).EntryId);
        }
        #endregion

        #region LookupEntry with context without profile
        public static Entry LookupEntry(this IDBEntryManager entryManager, COFEDB2 context, string fullParseName)
        {
            return entryManager.LookupEntry(context, COFEShared.AdminUserProfile, fullParseName);
        }
        
        public static Entry LookupEntry(this IDBEntryManager entryManager, COFEDB2 context, Guid entryId)
        {
            return entryManager.LookupEntry(context, COFEShared.AdminUserProfile, entryId);
        }
        #endregion
    
        public static void RemoveEntry(this IDBEntryManager entryManager, string fullParseName)
        {
            entryManager.RemoveEntry(StringUtils<IFileSystemInfo>.GetDeterministicGuid(fullParseName));
        }


        public static void ExpireEntry(this IDBEntryManager entryManager, string fullParseName, bool isEntryChanged, bool isListingChanged)
        {
            entryManager.ExpireEntry(StringUtils<IFileSystemInfo>.GetDeterministicGuid(fullParseName), isEntryChanged, isListingChanged);
        }

        public static Entry UpdateEntry(this IDBEntryManager entryManager, IPropertyProvider entryPP)
        {
            return COFEDBShared.DBOperations.UpdateDatabase((context) =>
                {                    
                    return entryManager.UpdateEntry(context, entryPP);
                    
                });
        }

    }
}
